import Vue from 'vue'
import MPDataManager from "@/public/MPDataManager.js";

const AuthorityUtil = {}
//判断是否有某个权限
AuthorityUtil.checkAuthority = function(code) {
	// return true
	if (code == null || code == "") {
		return true;
	}
	let authoritys = MPDataManager.getData("SYS_authority")
	for (let key in authoritys) {
		let a = authoritys[key];
		if (a.module_code == code || a.function_code == code) {
			return true;
		}
	}
	return false;
};

// 权限指令 如果不如何权限 这个组件会被移除
Vue.directive('authority', {
	// 当自己被添加到父级节点的时候触发
	inserted(el, binding, vnode) {
		let value = binding.value
		MPDataManager.addListener(el,"SYS_authority",function(){
			console.log("SYS_authority")
			console.log(value)
			if (!AuthorityUtil.checkAuthority(value)) {
				this.parentNode.removeChild(this);
			}
			MPDataManager.removeMyListener(this)
		})
	},
});

Vue.directive('authority', {
	// 当自己被添加到父级节点的时候触发
	inserted(el, binding, vnode) {
		let value = binding.value
		MPDataManager.addListener(el,"SYS_authority",function(){
			if (!AuthorityUtil.checkAuthority(value)) {
				this.parentNode.removeChild(this);
			}
			MPDataManager.removeMyListener(this)
		})
	},
});
// 项目权限的指令
// user_cfg修饰符检测用户编辑权限
//plan_cfg 修饰符检测计划管理权限
Vue.directive('project_authority', function(el, binding, vnode){
		let value = binding.value
		let project_authority = value.project_authority
		
		if(binding.modifiers.user_cfg)
		{
			if(!project_authority|| !project_authority.project_position ||  project_authority.project_position.authorityUserCfg!=1)
			{
				el.style.display = "none"
			}else{
				el.style.display = null
			}
		}
		else if(binding.modifiers.plan_cfg)
		{
			if(!project_authority|| !project_authority.project_position ||  project_authority.project_position.authorityPlanCfg!=1)
			{
				el.style.display = "none"
			}else{
				el.style.display = null
			}
		}
		else if(binding.modifiers.plan_item_cfg )
		{
			// 如果有系统级权限 则有编辑文件夹内部的权限
			if(project_authority && project_authority.project_position &&  project_authority.project_position.authorityPlanCfg==1)
			{
				el.style.display = null
			}else{
				// 如果有文件夹内部权限 则也可以编辑
				if(project_authority && project_authority.plan_authorities && project_authority.plan_authorities[value.plan_id] ==2)
				{
					el.style.display = null
				}
				else
				{
					el.style.display = "none"
				}
			}
			
		}
});



export default AuthorityUtil;
